package com.doctor.windflower_doctor.http.parser.impl;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.widget.Toast;

import com.alibaba.fastjson.serializer.JSONAwareSerializer;
import com.doctor.windflower_doctor.actionBeen.UserControlBeen;
import com.doctor.windflower_doctor.entity.RecordBeen;
import com.doctor.windflower_doctor.entity.UserBeen;

public class JsonHelper {
	public static List<UserControlBeen> userControl_Json(String json) {
		List<UserControlBeen> list = new ArrayList<UserControlBeen>();
		try {
			JSONObject jsonObject = new JSONObject(json);

			if (jsonObject.getBoolean("success")) {

				JSONObject jsonObject2 = jsonObject.getJSONObject("data");

				JSONArray jsonArray = jsonObject2.getJSONArray("groups");
				for (int i = 0; i < jsonArray.length(); i++) {
					JSONObject jsonObject3 = jsonArray.getJSONObject(i);
					UserControlBeen been = new UserControlBeen();
					been.setGroupName(jsonObject3.getString("groupName"));
					been.setId(jsonObject3.getString("_id"));
					been.setNumberOfUser(jsonObject3.getString("numberOfUser"));
					list.add(been);
				}
				return list;
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}

	public static UserControlBeen userControl_Add_Json(String json) {

		try {
			JSONObject jsonObject = new JSONObject(json);
			if (jsonObject.getString("success").equals("true")) {
				JSONObject jsonObject2 = jsonObject.getJSONObject("data");
				UserControlBeen controlBeen = new UserControlBeen();
				controlBeen.setId(jsonObject2.getString("_id"));
				controlBeen.setGroupName(jsonObject2.getString("groupName"));
				controlBeen.setNumberOfUser(jsonObject2
						.getString("numberOfUser"));

				return controlBeen;
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;

	}

	public static String userControl_Delete_Json(String json) {
		JSONObject jsonObject;

		try {
			jsonObject = new JSONObject(json);
			return jsonObject.getString("success");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return json;

	}

	// ========{
	// "success": true,
	// "data": {
	// "pageCount": 10,
	// "lists": [
	// {
	// "_id": "544b1a5dc5684b95c231283d",
	// "createdAt": 1414108800000,
	// "weight": 60
	// }
	// ]
	// },
	// "code": 200,
	// "message": ""
	// }
	public static List<RecordBeen> record_Json(String json) {
		JSONObject jsonObject;
		JSONObject jsonObject2;
		List<RecordBeen> list;

		try {
			jsonObject = new JSONObject(json);
			boolean state = jsonObject.getBoolean("success");
			if (state) {
				list = new ArrayList<RecordBeen>();

				jsonObject2 = jsonObject.getJSONObject("data");
				JSONArray jsonArray = jsonObject2.getJSONArray("lists");
				for (int i = 0; i < jsonArray.length(); i++) {
					JSONObject jsonObject3 = jsonArray.getJSONObject(i);
					RecordBeen been = new RecordBeen();
					been.set_id(jsonObject3.getString("_id"));
					been.setCreatedAt(jsonObject3.getString("createdAt"));
					been.setWeight(jsonObject3.getString("weight"));
					been.setTemperature(jsonObject3.optString("temperature"));
					been.setBellyLength(jsonObject3.optString("bellyLength"));
					been.setExerciseTime(jsonObject3.optString("exerciseTime"));
					been.setFetalMovement(jsonObject3
							.optString("fetalMovement"));
					been.setSleepTime(jsonObject3.optString("SleepTime"));
					list.add(been);
				}
				return list;
			} else {
				return null;
			}

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}

}
